package org.springblade.task.job.report.core;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.CommonUtil;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.coreBoard.entity.DailyCoreDataReport;
import org.springblade.modules.coreBoard.service.IDailyCoreDataReportService;
import org.springblade.task.base.entity.BaseUser;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.param.BaseParam;
import org.springblade.task.base.param.BaseShowLogParam;
import org.springblade.task.base.param.BaseUserParam;
import org.springblade.task.base.service.*;
import org.springblade.task.base.vo.BaseAudioChapterOrderVO;
import org.springblade.task.base.vo.BaseEbookOrderVO;
import org.springblade.task.base.vo.BaseNetChapterOrderVO;
import org.springblade.task.base.vo.BaseVirtualMoneyFlowLogVO;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Title: 核心数据看板
 * @version V1.0
 */
@Component
public class DailyCoreDataReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseUserService baseUserService;
	@Autowired
	private IBaseVipUserService baseVipUserService;
	@Autowired
	private IBaseShowLogService baseShowLogService;
	@Autowired
	private IBaseUserVisitLogService baseUserVisitLogService;
	@Autowired
	private IBaseVirtualMoneyFlowLogService baseVirtualMoneyFlowLogService;
	@Autowired
	private IBaseEbookOrderService baseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService baseAudioChapterOrderService;
	@Autowired
	private IBaseNetChapterOrderService baseNetChapterOrderService;

	@Autowired
	private IDailyCoreDataReportService dailyCoreDataReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("DailyCoreDataReportJob")
	public ReturnT<String> DailyCoreDataReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailyCoreDataReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailyCoreDataReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {
		// 查询用户数
		BaseUserParam userParam = new BaseUserParam();
		userParam.setTableName(CommonUtil.getTableName(new BaseUser()));
		userParam.setToday(DateUtils.getDayEndTime(taskDate));
		Integer userCount = baseUserService.queryUserCount(userParam);
		// VIP用户数
		List<Integer> serviceStatusList = Arrays.asList(0,1,2);
		userParam.setServiceStatusList(serviceStatusList);
		Integer vipUserCount = baseVipUserService.queryVipUserCount(userParam);

		// 激活VIP用户数
		List<Integer> serviceStatusList2 = Arrays.asList(1,2);
		userParam.setServiceStatusList(serviceStatusList2);
		Integer activateVipUserCount = baseVipUserService.queryVipUserCount(userParam);

		String startTime = DateUtils.getDateStartTime(taskDate);
		String endTime = DateUtils.getDayEndTime(taskDate);
		BaseShowLogParam logParam = new BaseShowLogParam();
		logParam.setStartDate(startTime);
		logParam.setEndDate(endTime);
		// 日PV
		Integer pvCount = baseShowLogService.queryDailyPVCount(logParam);
		//日UV
		Integer uvCount = baseShowLogService.queryDailyUVCount(logParam);
		// 新增登陆用户：今日首次登陆的用户
		userParam.setStartDate(startTime);
		userParam.setEndDate(endTime);
		Integer firstLoginUserCount = baseUserService.queryTodayFirstLoginUserCount(userParam);

		// 查询活跃用户数
		Integer activeUsersCount  = baseUserVisitLogService.queryActiveUsersCount(userParam);

		BaseParam param = new BaseParam();
		param.setStartDate(DateUtils.getDateStartTime(taskDate));
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		// 充值
		BaseVirtualMoneyFlowLogVO virtualMoneyFlowLog =  baseVirtualMoneyFlowLogService.queryTodayRechargeAmount(param);

		BaseOrderBookParam bookParam = new BaseOrderBookParam();
		bookParam.setStartDate(DateUtils.getDateStartTime(taskDate));
		bookParam.setEndDate(DateUtils.getDayEndTime(taskDate));
		// 电子书订单
		BaseEbookOrderVO ebookOrderVO = baseEbookOrderService.queryTodayOrderAmount(bookParam);
		// 有声订单
		BaseAudioChapterOrderVO audioChapterOrderVO  = baseAudioChapterOrderService.queryTodayOrderAmount(bookParam);
		// 网文订单
		BaseNetChapterOrderVO netChapterOrderVO = baseNetChapterOrderService.queryTodayOrderAmount(bookParam);

		int payingUsers = 0; // 日付费用户数
		int repurchaseUsers = 0; // 日复购用户数
		int dailySales = 0; // 日销售额
		BigDecimal customerPrice = BigDecimal.ZERO; // 客单价
		int dailySalesVolume = 0; // 日销量
		int dailyRecharge = 0; // 日充值
		int dailyIncome = 0; // 日收入
		if(null != virtualMoneyFlowLog) {
			dailyRecharge = dailyRecharge + virtualMoneyFlowLog.getRechargeAmount();
		}
		if(null != ebookOrderVO) {
			payingUsers = payingUsers + ebookOrderVO.getPinNum();
			dailySales = dailySales + ebookOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + ebookOrderVO.getOrderNum();
		}
		if(null != audioChapterOrderVO) {
			payingUsers = payingUsers + audioChapterOrderVO.getPinNum();
			dailySales = dailySales + audioChapterOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + audioChapterOrderVO.getOrderNum();
		}
		if(null != netChapterOrderVO) {
			payingUsers = payingUsers + netChapterOrderVO.getPinNum();
			dailySales = dailySales + netChapterOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + netChapterOrderVO.getOrderNum();
		}
		dailyIncome = dailyRecharge + dailySales;
		customerPrice = new BigDecimal(dailySales).divide(new BigDecimal(payingUsers));

		DailyCoreDataReport coreDataReport = new DailyCoreDataReport();
		coreDataReport.setDate(taskDate);
		coreDataReport.setUserNum(userCount);
		coreDataReport.setVipUserNum(vipUserCount);
		coreDataReport.setActiveVipUserNum(activateVipUserCount);
		coreDataReport.setPv(pvCount);
		coreDataReport.setUv(uvCount);
		coreDataReport.setNewUserNum(firstLoginUserCount);
		coreDataReport.setActiveUserNum(activeUsersCount);

		coreDataReport.setPayUserNum(payingUsers);  // 日付费用户数
//		coreDataReport.setRepurchaseUsers(repurchaseUsers); // 日复购用户数
		coreDataReport.setSalesAmount(dailySales);// 日销售额
		coreDataReport.setUnitPrice(customerPrice); // 客单价
		coreDataReport.setSalesVolume(dailySalesVolume); // 日销量
		coreDataReport.setRecharge(dailyRecharge); // 日充值
		coreDataReport.setIncome(dailyIncome); // 日收入

		dailyCoreDataReportService.save(coreDataReport);

	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
